﻿//<script language="javascript" src="rapi/rbase/RFunctions.js"></script>

importClass('rapi.rbase.RDiv');
importClass('rapi.rbase.RImage');
importClass('rapi.rbase.RButton');
importClass('rapi.rbase.RTextField');
importClass('rapi.rbase.RCategoryPicker');

function RSearchRoundBox(imgsrc){
    this.imgsrc=imgsrc;	
	this.toString=function(){
		return 'RSearchRoundBox';
	}
	///具名常量
	this.MAX_RADIUS=3500;//最大搜索的范围，单位：米
	this.MAX_RESULT_COUNT=200;//最多显示搜索结果的信息点个数
	///搜索周边的中心点经纬度
	this.centerLat=23.00001;
	this.centerLng=113.00001;
	this.searchRadius=500;
	
	this.setCenterLat=function(nCenterLat){
	    this.centerLat=nCenterLat;
	}
	this.setCenterLng=function(nCenterLng){
	    this.centerLng=nCenterLng;
	}
	this.setSearchRadius=function(nSearchRadius){
	    if(nSearchRadius>0&&nSearchRadius<this.MAX_RADIUS){
	        this.searchRadius=nSearchRadius;
	    }
	}
	///获得搜索的范围
	this.getSearchBounds=function(){
	    if(this.radiusDropList.getValue()>0){
	        this.searchRadius=this.radiusDropList.getValue();
	    }else{
	        this.searchRadius=this.MAX_RADIUS;
	    }
	    return getBoundsByCenterLatLngAndRadius(this.centerLat,this.centerLng,this.searchRadius);
	} 
    ///获得需要搜索的关键字
	this.getSearchKeyword=function(){
	    return trim(this.kwText.getText());
	}
	///获得需要搜索的类别ID
	this.getSearchCategoryId=function(){
	    return this.CategoryPicker.getCurrentCategoryId();
	}
	///获得需要搜索的类别名称
	this.getSearchCategoryName=function(){
	    return this.CategoryPicker.getCurrentCategoryName();
	}
	///获得需要搜索结果的个数
	this.getSearchResultCount=function(){
	    if(this.resultCountDropList.getValue()>0)
	    {
    	    return this.resultCountDropList.getValue()
	    }else{
	        return this.MAX_RESULT_COUNT;
	    }
	}

	///背景的大小
	this.bgWidth=1024;
	this.bgHeight=568;
	///搜索框的大小
	this.innerWidth=380;
	this.innerHeight=200;
	///搜索的位置
	this.innerLeft=this.bgWidth/3;
	this.innerTop=this.bgHeight/3;
	
	//------begin 页面添加元素-------------

	///背景的DIV	
	this.bgDiv=new RDiv(0,0,null,null).get();
	this.bgStyle=this.bgDiv.style;
	///内部的DIV
	this.innerDiv=new RDiv(0,0,null,null).get();
	this.innerStyle=this.innerDiv.style;
	this.bgDiv.appendChild(this.innerDiv);
	///添加分类的下拉列表
    this.CategoryPicker=new RCategoryPicker(0,3);
    this.categoryStyle=this.CategoryPicker.style;
    this.bgDiv.appendChild(this.CategoryPicker.get());

	///添加关闭按纽到DIV中
	if(!this.closeBtn){
		this.closeBtn=new RImage(this.innerWidth,13,this.imgsrc+'/close.gif').get();
	}
	this.closeBtn.style.cursor='pointer';
	this.closeBtn.root=this;
	this.bgDiv.appendChild(this.closeBtn);
	///点击关闭事件
	this.closeBtn.onclick=function(){
		this.root.hide();
	}
	//添加搜索按纽到DIV中
	if(!this.submitBtn){
	    this.submitBtn=new RButton(0,0,56,24,'搜索');
	}
	this.submitBtn.get().root=this;
	this.bgDiv.appendChild(this.submitBtn.get());
	
	this.submitBtn.get().onclick=function(){
	    this.root.notify('onBoxClicked');
	    this.root.hide();
	}
	///添加关键字搜索框到DIV中
	if(!this.kwText){
	    this.kwText=new RTextField(0,0,250,'');
	    this.kwText.setAcceptButton(this.submitBtn.get());
	}
	this.bgDiv.appendChild(this.kwText.get());
	///添加周边范围大小下拉列表（单位：米）
    if(!this.radiusDropList){
        this.radiusDropList=new RDropList(0,0,50,null);
        this.radiusDropList.addListItem('不限',-1);
        for(var cRadius=100;cRadius<=this.MAX_RADIUS;){
            this.radiusDropList.addListItem(cRadius,cRadius);
            cRadius *= 2;
        }
    }	
	this.bgDiv.appendChild(this.radiusDropList.get());
	///添加搜索结果显示个数下拉列表（单位：个）
    if(!this.resultCountDropList){
        this.resultCountDropList=new RDropList(0,0,50,null);
        this.resultCountDropList.addListItem('不限',-1);
        for(var cCount=5;cCount<=this.MAX_RESULT_COUNT;){
            this.resultCountDropList.addListItem(cCount,cCount);
            cCount *= 2;
        }
    }	
	this.bgDiv.appendChild(this.resultCountDropList.get());
	//------end  页面添加元素-------------
	this.setInnerHTML=function(){
        this.setAllElementLocation();
       
		this.innerDiv.innerHTML= '<table style="width: '+this.bgWidth+'px; height: '+this.bgHeight+'px;"><tr><td style="z-index: 2000; background-color: Menu; filter: alpha(opacity=70);border-right: activecaption thick ridge; border-top: activecaption thick ridge;border-left: activecaption thick ridge; border-bottom: activecaption thick ridge;"><table><tr><td valign="top" style="z-index: 2000; background-color: Menu; filter: alpha(opacity=70);width: '+this.innerWidth+'px; height: '+this.innerHeight+'px;position:absolute;left:'+this.innerLeft+';top:'+this.innerTop+'; border-right: activecaption thick ridge; border-top: activecaption thick ridge;border-left: activecaption thick ridge; border-bottom: activecaption thick ridge;"><table><tr style="height:40px" valign="middle" ><td>分&nbsp;&nbsp;类：</td></tr><tr style="height:40px" valign="middle" ><td>关键字：</td></tr><tr style="height:40px" valign="middle" ><td>范&nbsp;&nbsp;围：&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;米<span style="color: #999999">&nbsp; 请选择你希望搜索的半径范围！</span></td></tr><tr style="height:40px" valign="middle" ><td>个&nbsp;&nbsp;数：&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;个<span style="color: #999999">&nbsp; 请设置搜索结果显示的信息点个数！</span></td></tr></table></td></tr></table></td></tr></table>';
	} 
	
	///获得整个搜索框的DIV
	this.get=function(){
		return this.bgDiv;
	}
	
	///重新布局所有元素；必须是先确定版面（背景与搜索框），再可以确定其他元素的布局
	this.setAllElementLocation=function(){
		if(this.closeBtn){
		    if(this.innerLeft+this.innerWidth-8>0){
		        this.closeBtn.style.left=this.innerLeft+this.innerWidth-8;
		    }
			this.closeBtn.style.top=this.innerTop+6;
		}
		if(this.submitBtn){
		    if(this.innerLeft+this.innerWidth-60>0){
		        this.submitBtn.style.left=this.innerLeft+this.innerWidth-60;
		    }
			this.submitBtn.style.top=this.innerTop+58;
		}
		if(this.kwText){
		    if(this.innerLeft+60>0){
		        this.kwText.style.left=this.innerLeft+60;
		    }
			this.kwText.style.top=this.innerTop+60;
		}
		if(this.radiusDropList){
		    if(this.innerLeft+60>0){
		        this.radiusDropList.style.left=this.innerLeft+60;
		    }
			this.radiusDropList.style.top=this.innerTop+100;
		}
		if(this.resultCountDropList){
		    if(this.innerLeft+60>0){
		        this.resultCountDropList.style.left=this.innerLeft+60;
		    }
			this.resultCountDropList.style.top=this.innerTop+140;
		}
		this.categoryStyle.left=this.innerLeft+60;
		this.categoryStyle.top=this.innerTop+20;
		if(this.innerWidth-60>0)
		{
		    this.categoryStyle.width=this.innerWidth-60;
		}
		this.categoryStyle.height=this.innerHeight;
    }
	
	///设置搜索框的位置
	this.setBoxLocation=function(nBoxLeft,nBoxTop){
		this.bgStyle.left=0;
		this.bgStyle.top=0;
		if(nBoxLeft&&nBoxLeft>0&&nBoxLeft<this.bgWidth){
		    this.innerLeft=nBoxLeft;
		}
		if(nBoxTop&&nBoxTop>0&&nBoxTop<this.bgHeight){
		    this.innerTop=nBoxTop;
		}
		this.setInnerHTML(); 
	}
    ///设置搜索框的大小,背景的大小
	this.setBoxSize=function(nBoxWidth,nBoxHeight,nBgWidth,nBgHeight){
	    if(nBgWidth&&nBgWidth>0){
	        this.bgWidth=nBgWidth;
		}
		if(nBgHeight&&nBgHeight>0){
		    this.bgHeight=nBgHeight;
		}
		if(nBoxWidth&&nBoxWidth>0&&nBoxWidth<nBgWidth){
		    this.innerWidth=nBoxWidth;
		}
		if(nBoxHeight&&nBoxHeight>0&&nBoxHeight<nBgHeight){
		    this.innerHeight=nBoxHeight;
		}
		this.setInnerHTML(); 
	}
	
	this.resize=function(nBgWidth,nBgHeight){
	    if(nBgWidth>=0&&nBgHeight>=0){
//	    alert(nBgWidth);
//	    alert(nBgHeight);
	        this.setBoxSize(null,null,nBgWidth,nBgHeight);
	        this.setBoxLocation(Math.floor(nBgWidth/3),Math.floor(nBgHeight/3));
	    }
	    this.notify('onBoxResized');
	}
	
	///隐藏整个搜索框
	this.hide=function(){
		this.bgStyle.visibility='hidden';
		this.notify('onBoxClosed');
	}
	///显示整个搜索框
	this.show=function(){
		this.bgStyle.visibility='visible';
		this.setInnerHTML(); 
		this.notify('onBoxOpened');
	}
		
	///添加各种响应事件
	//响应盒子关闭事件
	this.onBoxClosed=new Array();
	//响应盒子打开事件	
	this.onBoxOpened=new Array();
	//响应按纽确定事件
	this.onBoxClicked=new Array();
	//响应盒子重新调整大小事件
	this.onBoxResized=new Array();
	
	this.addListener=function(listener, estring){
		switch(estring){
			case 'onBoxClosed':
				if(listener.onBoxClosed){
					this.onBoxClosed[this.onBoxClosed.length]=listener;
				}else{
					window.status=this.toString()+':不合法的'+estring+'事件对象';
				}
			break;
		    case 'onBoxOpened':
				if(listener.onBoxOpened){
					this.onBoxOpened[this.onBoxOpened.length]=listener;
				}else{
					window.status=this.toString()+':不合法的'+estring+'事件对象';
				}
		    break;
			case 'onBoxClicked':
				if(listener.onBoxClicked){
					this.onBoxClicked[this.onBoxClicked.length]=listener;
				}else{
					window.status=this.toString()+':不合法的'+estring+'事件对象';
				}
			break;
			case 'onBoxResized':
				if(listener.onBoxResized){
					this.onBoxResized[this.onBoxResized.length]=listener;
				}else{
					window.status=this.toString()+':不合法的'+estring+'事件对象';
				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	
	this.notify=function(estring){
		switch(estring){
			case 'onBoxClosed':
				for(var i=0;i<this.onBoxClosed.length;i++){
					if(this.onBoxClosed[i].onBoxClosed){
						this.onBoxClosed[i].onBoxClosed();
					}else{
						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
					}
				}
			break;
			case 'onBoxOpened':
				for(var i=0;i<this.onBoxOpened.length;i++){
					if(this.onBoxOpened[i].onBoxOpened){
						this.onBoxOpened[i].onBoxOpened();
					}else{
						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
					}
				}
			break;
			case 'onBoxClicked':
				for(var i=0;i<this.onBoxClicked.length;i++){
					if(this.onBoxClicked[i].onBoxClicked){
						this.onBoxClicked[i].onBoxClicked();
					}else{
						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
					}
				}
			break;
			case 'onBoxResized':
				for(var i=0;i<this.onBoxResized.length;i++){
					if(this.onBoxResized[i].onBoxResized){
						this.onBoxResized[i].onBoxResized();
					}else{
						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
					}
				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	
	///初始化时先是隐藏整个界面
	this.hide();

	this.getCenterLatLng=function(){
		return new RLatLng(this.centerLat,this.centerLng);
	}
}